/**
 * 
 */
package com.xbang.core.utils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**
 * Date related utilities
 * 
 * @author dev.zhang
 */
public abstract class DateUtils {

	private static final String DEFAULT_DATE_PATTERN = "yyyy-MM-dd";

	/**
	 * 加
	 * 
	 * @param date
	 * @param iday
	 * @param addEnum
	 *            cal.Day_OF_YEAR
	 * @return
	 */
	public static Date addDateEnum(Date date, int iday, int addEnum) {
		Calendar cal = Calendar.getInstance();
		cal.setTime(date);
		cal.add(addEnum, iday);
		return cal.getTime();
	}

	/**
	 * 加天
	 * 
	 * @param date
	 * @param iday
	 * @return
	 */
	public static Date addDay(Date date, int iday) {
		Calendar cal = Calendar.getInstance();
		cal.setTime(date);
		cal.add(cal.DAY_OF_YEAR, iday);
		return cal.getTime();
	}

	/**
	 * 加月
	 * 
	 * @param date
	 * @param imonth
	 * @return
	 */
	public static Date addMonth(Date date, int imonth) {
		Calendar cal = Calendar.getInstance();
		cal.setTime(date);
		cal.add(cal.MONTH, imonth);
		return cal.getTime();
	}

	/**
	 * data add day by iday - String
	 * 
	 * @param date
	 * @param iday
	 * @param dataPatten
	 * @return
	 */
	public static String addDay(String date, int iday, String dataPatten) {
		SimpleDateFormat format = new SimpleDateFormat(dataPatten);
		Date d = null;
		try {
			d = format.parse(date);
		} catch (Exception e) {
			e.printStackTrace();
		}
		Calendar cal = Calendar.getInstance();
		cal.setTime(d);
		cal.add(cal.DAY_OF_YEAR, iday);
		return format.format(cal.getTime());
	}

	/**
	 * Do a wrap of {@link org.apache.commons.lang.time.DateUtils#parseDate(String, String[])}
	 * 
	 * @param date
	 * @param pattern
	 * @return
	 * 
	 * @see {@link org.apache.commons.lang.time.DateUtils#parseDate(String, String[])}
	 */
	public static Date parseDate(String dateString, String pattern) {
		return parseDate(dateString, new String[] { pattern });
	}

	/**
	 * Do a wrap of {@link org.apache.commons.lang.time.DateUtils#parseDate(String, String[])}
	 * 
	 * @param date
	 * @param patterns
	 * @return
	 * 
	 * @see {@link org.apache.commons.lang.time.DateUtils#parseDate(String, String[])}
	 */
	public static Date parseDate(String dateString, String[] patterns) {
		try {
			return org.apache.commons.lang.time.DateUtils.parseDate(dateString, patterns);
		} catch (ParseException e) {
			return null;
		}
	}

	/**
	 * Parse date using DEFAULT_DATE_PATTERN
	 * 
	 * @param year
	 * @param month
	 * @param date
	 * @param pattern
	 * @return
	 */
	public static Date parseDate(int year, int month, int date) {
		return parseDate(format(year, month, date), DEFAULT_DATE_PATTERN);
	}

	/**
	 * Get current time millis as a string
	 * 
	 * @return
	 */
	public static String getCurrentTimeMillis() {
		return String.valueOf(System.currentTimeMillis());
	}

	/**
	 * Get first month day for calendar. There are differences between the first day of the month and the first day for
	 * calendar.
	 * 
	 * eg. First day for calendar for 2008.5.18 is 2008.4.27
	 * 
	 * @param date
	 * @return
	 */
	public static Date getFirstMonthDayForCalendar(Date date) {

		Calendar calendar = Calendar.getInstance();
		calendar.setTime(date);
		calendar.set(Calendar.DAY_OF_MONTH, 1);

		// Set first day of week is Sunday
		calendar.setFirstDayOfWeek(0);

		calendar.add(Calendar.DAY_OF_YEAR, -(calendar.get(Calendar.DAY_OF_WEEK) - 1));
		return calendar.getTime();
	}

	/**
	 * Get last month day for calendar. There are differences between the last day of the month and the last day for
	 * calendar.
	 * 
	 * eg. Last day for calendar for 2008.6.18 is 2008.7.5
	 * 
	 * @param date
	 * @return
	 */
	public static Date getLastMonthDayForCalendar(Date date) {
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(date);
		calendar.set(Calendar.DAY_OF_MONTH, calendar.getActualMaximum(Calendar.DAY_OF_MONTH));

		// Set first day of week is Sunday
		calendar.setFirstDayOfWeek(0);

		calendar.add(Calendar.DAY_OF_YEAR, Calendar.DAY_OF_WEEK - calendar.get(Calendar.DAY_OF_WEEK));
		return calendar.getTime();
	}

	/**
	 * Get the days of month
	 * 
	 * @param year
	 * @param month
	 * @return
	 */
	public static int getMonthDay(int year, int month) {
		Calendar calendar = Calendar.getInstance();
		calendar.set(year, month - 1, 1);
		return calendar.getActualMaximum(Calendar.DAY_OF_MONTH);
	}

	/**
	 * Format date according to year, month and date to DEFAULT_DATE_PATTERN
	 * 
	 * @param year
	 * @param month
	 * @param date
	 * @return
	 */
	public static String format(int year, int month, int date) {
		Calendar calendar = Calendar.getInstance();
		calendar.set(year, month - 1, date);
		return new SimpleDateFormat("yyyy-MM-dd").format(calendar.getTime());
	}

	/**
	 * Format date according to input date to DEFAULT_DATE_PATTERN
	 * 
	 * @param date
	 * @return
	 */
	public static String format(Date date) {
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(date);
		return new SimpleDateFormat("yyyy-MM-dd").format(calendar.getTime());
	}

	/**
	 * Format date according to input date to patten
	 * 
	 * @param date
	 * @param patten
	 * @return
	 */
	public static String format(Date date, String patten) {
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(date);
		return new SimpleDateFormat(patten).format(calendar.getTime());
	}

	/**
	 * Get year of the date
	 * 
	 * @param date
	 * @return
	 */
	public static int getYear(Date date) {
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(date);
		return calendar.get(Calendar.YEAR);
	}

	/**
	 * Get month of the date
	 * 
	 * @param date
	 * @return
	 */
	public static int getMonth(Date date) {
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(date);
		return calendar.get(Calendar.MONTH) + 1;
	}

	/**
	 * Get day of the date
	 * 
	 * @param date
	 * @return
	 */
	public static int getDay(Date date) {
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(date);
		return calendar.get(Calendar.DATE);
	}

}
